from django.contrib.auth import logout
from accounts.forms import UserRegistrationForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.shortcuts import render_to_response
from django.template import RequestContext

def home(request):
	'''
	lib-ms home page
	'''
	return render_to_response('accounts/home.html', {}, RequestContext(request))

def profile(request):
	return HttpResponseRedirect('/readers/%s/' % request.user.id)

def register(request):
	'''
	For user registration
	'''
	logout(request) # logout firstly
	if request.method == 'POST':
		_form = UserRegistrationForm(request.POST)
		if _form.is_valid():
			new_user = _form.save()
			return HttpResponseRedirect('/accounts/login/?next=/')
	else:
		_form = UserRegistrationForm()

	context = {'form': _form}
	context.update(csrf(request))

	return render_to_response('accounts/register.html', context)
